<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Officer;
use App\Model\Jobads;

class OfficerController extends Controller
{
    //
    public function getOfficer(Request $request){
        return response([
            'data' => Officer::get(),
            'code' => 200
        ]);
    }

    public function getJobads(Request $request){
        return response([
            'data' => Jobads::get(),
            'code' => 200
        ]);
    }

    public function addOfficer(Request $request){
        $data = $request->data;
        foreach($data['add']??[] as $key => $item){
            Officer::create([
                'number' => $item['number'],
                'name' => $item['name'],
                'job' => $item['job'],
                'certificate' => $item['certificate'],
                'require' => $item['require'],
                'obode' => $item['obode'],
                'date' => $item['date'],
            ]);
        }
        Officer::destroy(array_pluck($data['remove']??[],'id'));
        foreach($data['update']??[] as $key => $item){
            Officer::find($item['id'])->update([
                'number' => $item['number'],
                'name' => $item['name'],
                'job' => $item['job'],
                'certificate' => $item['certificate'],
                'require' => $item['require'],
                'obode' => $item['obode'],
                'date' => $item['date'],
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }

    public function addJobads(Request $request){
        $data = $request->data;
        foreach($data['add']??[] as $key => $item){
            Jobads::create([
                'number' => $item['number'],
                'job' => $item['job'],
                'certificate' => $item['certificate'],
                'area' => $item['area'],
                'ship_type' => $item['ship_type'],
                'recruitment' => $item['recruitment'],
                'date' => $item['date'],
            ]);
        }
        Jobads::destroy(array_pluck($data['remove']??[],'id'));
        foreach($data['update']??[] as $key => $item){
            Jobads::find($item['id'])->update([
                'number' => $item['number'],
                'job' => $item['job'],
                'certificate' => $item['certificate'],
                'area' => $item['area'],
                'ship_type' => $item['ship_type'],
                'recruitment' => $item['recruitment'],
                'date' => $item['date'],
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }
}
